package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @BelongsProject: itcast_health
 * @BelongsPackage: com.itheima.health.controller
 * @Author: ${Shiu}
 * @CreateTime: 2020-02-22 20:08
 * @Description: 角色控制层
 */
@Slf4j
@RequestMapping("/role")
@RestController
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 编辑角色
     * @param role
     * @param permissionIds
     * @param menuIds
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds){
        try {
            roleService.edit(role,permissionIds,menuIds);
            return new Result(true, MessageConst.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.EDIT_ROLE_FAIL);
        }

    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.deleteRoleById(id);
            return new Result(true, MessageConst.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.DELETE_ROLE_FAIL);
        }
    }

    /**
     * 新增角色
     * @param role
     * @param permissionIds
     * @param menuIds
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds){
        try {
            roleService.add(role,permissionIds,menuIds);
            return new Result(true, MessageConst.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConst.ADD_ROLE_FAIL);
        }
    }

    /**
     * 根据角色id查询套餐id集合
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){
        try {
            List<Integer> menuIds = roleService.findMenuIdsById(id);
            return new Result(true, MessageConst.QUERY_MENU_SUCCESS,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConst.QUERY_MENU_FAIL);
        }
    }

    /**
     * 根据角色id查询权限id集合
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try {
            List<Integer> permissionIds = roleService.findPermissionIdsById(id);
            return new Result(true, MessageConst.QUERY_PERMISSION_SUCCESS,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 根据角色id查询角色
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("/findById")
    public Result findById(Integer id){

        try {
            Role role = roleService.findById(id);
            return new Result(true, MessageConst.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.QUERY_ROLE_FAIL);
        }

    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("queryPageBean==========={}",queryPageBean);
        try {
            PageResult pageResult = roleService.pageQuery(queryPageBean);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L,null);
        }
    }


    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> list =roleService.findAll();
            return new Result(true, MessageConst.ACTION_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }
}
